/* (C) 2007-2009 caitong Limited
 *
 * Authors:
 * legs.ant.king@gmail.com


   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
//#include "../include/unistd.h"
#include "../include/SentMsg.h"
#include "../include/InitVariable.h"
#include "../include/Conn.h"
#include <iostream>
#include <string.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#define MaxList 50
using namespace std;



char** iInitConext(string ContextLine,char** ContextList, int &listcnt);


int SentMsg(char SentContext[180],char iCompasion[][50],char iArgument[][50])
{
	//char ContextList[][15]={0};
	char **ppContextList=NULL;
	int  listlen=0;

	int PhoneLine=GetMatachChar(iCompasion, "phone");
	string ContextLine=iArgument[PhoneLine];
	int SPhone=GetMatachChar(iCompasion, "sphone");
	int PhonePwd=GetMatachChar(iCompasion, "phonepwd");
	int MaxWarningValue=GetMatachChar(iCompasion, "MaxWarningValue");
	ppContextList=iInitConext(ContextLine,ppContextList, listlen);
//	cout<<"Context messages contact is :"<<x<<endl;
	for(int i=0;i<listlen;i++)
	{
		char systemsr[200]={0};
		//cout<<ppContextList[i]<<endl;

//		sprintf(systemsr,"/root/sendmsg/yxdx/bin/p_yx_send_yd %s \"%s\" ", ppContextList[i], SentContext);
//      http://sms.api.bz/fetion.php?username=&password=&sendto=&message='hello world'
		sprintf(systemsr,"wget \"http://sms.api.bz/fetion.php?username=%s&password=%s&sendto=%s&message=%s\" ", iArgument[SPhone] ,iArgument[PhonePwd] ,ppContextList[i], SentContext);
		WriteLog(iArgument,iCompasion,systemsr);
		cout<<systemsr<<endl;
		system(systemsr);
	}
	return 0;
}

char** iInitConext(string ContextLine,char** ContextList, int &listcnt)
{

	int iIndexCvs=ContextLine.find_first_of(',',0);//µÚÒ»¸ö¶ººÅÎ»ÖÃ
	if(string::npos==iIndexCvs)
	{			
		ContextList = new char*[1];
		ContextList[0] = new char[15];
		listcnt = 1;
		strcpy(ContextList[0],ContextLine.c_str());
		return ContextList;
	}
	else
	{
		int iCvsCount=1;
		int iCvsLastLocation=ContextLine.find_last_of(','); //×îºó¶ººÅµÄÎ»ÖÃ
		ContextList = new char*[iCvsLastLocation];	
		
		if(iIndexCvs==iCvsLastLocation)
		{
			ContextList[0]=new char[15];
			ContextList[1]=new char[15];
			string iLeft=ContextLine.substr(0,iIndexCvs);
			strcpy(ContextList[0],iLeft.c_str());
			int iiIndexCvs=iIndexCvs; 
			++iiIndexCvs; //µÚ¶þ¸öµÄ¿ªÊ¼Î»ÖÃ
			string iRight=ContextLine.substr(iiIndexCvs);
			strcpy(ContextList[1],iRight.c_str());
			listcnt = 2;
			return ContextList;
		}
		else
		{
				//¶ººÅ×ÜÊý
			int iDiff[MaxList]={0}; //¶ººÅÓë¶ººÅÖ®ÆÚ¼äµÄ²îÖµ
			int iLocation[MaxList]={0}; //¶ººÅµÄ¾ßÌåÎ»ÖÃ
			iDiff[0]=iIndexCvs;
			iLocation[0]=0;
			int iCvsLocation=iIndexCvs;
			int iMoreds=iIndexCvs; 
			++iMoreds;	//µÚÒ»¸ö¶ººÅµ±Ç°Î»ÖÃ¼ÓÒ»
			for(int i=1;i<MaxList;i++)
			{
				iCvsLocation=iMoreds;
				int CrrentCvsId=ContextLine.find_first_of(',',iCvsLocation);
				if(string::npos==CrrentCvsId)
				{
					break;
				}
				if(CrrentCvsId<=iCvsLastLocation)
				{
					
					iDiff[i]=CrrentCvsId-iCvsLocation;
					iLocation[i]=iCvsLocation;
					iMoreds=CrrentCvsId;
					++iMoreds;
					++iCvsCount;
				}
				else
				{
					break;
				}
			}
			//++iCvsCount;
			for(int i=0;i<=iCvsCount;i++)
			{
				if(i==iCvsCount)
				{
					int xbb=iCvsLastLocation;
					++xbb;
					string iCurrentValue=ContextLine.substr(xbb);
					ContextList[i] = new char[15];
					strcpy(ContextList[i],(char *)iCurrentValue.c_str());
				}
				else if(iLocation[i]<iCvsLastLocation )
				{
					string iCurrentValue=ContextLine.substr(iLocation[i],iDiff[i]);
					ContextList[i] = new char[15];
					strcpy(ContextList[i],(char *)iCurrentValue.c_str());
				}
			}
			listcnt = iCvsCount;
			return ContextList;
		}
	
	}
}



